#!/bin/bash
# 机器全面检查脚本 v1.0
# 功能：深度检查系统配置、性能状态和潜在问题

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'

# 全局变量
CHECKUP_DIR="/tmp/machine_checkup_$(date +%Y%m%d_%H%M%S)"
SUMMARY_FILE="$CHECKUP_DIR/summary.txt"
DETAIL_REPORT="$CHECKUP_DIR/detailed_report.txt"
ISSUES_FILE="$CHECKUP_DIR/issues_found.txt"

# 初始化
init_checkup() {
    mkdir -p "$CHECKUP_DIR"
    echo "=== 机器检查报告 ===" > "$SUMMARY_FILE"
    echo "检查时间: $(date)" >> "$SUMMARY_FILE"
    echo "生成目录: $CHECKUP_DIR" >> "$SUMMARY_FILE"
    echo "" >> "$SUMMARY_FILE"
    
    echo "=== 详细检查报告 ===" > "$DETAIL_REPORT"
    echo "=== 发现问题汇总 ===" > "$ISSUES_FILE"
}

# 输出函数
log() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; echo "WARN: $1" >> "$ISSUES_FILE"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; echo "ERROR: $1" >> "$ISSUES_FILE"; }
info() { echo -e "${BLUE}[INFO]${NC} $1"; }
detail() { echo -e "${CYAN}[DETAIL]${NC} $1"; }

# 1. 系统基本信息检查
check_system_basic() {
    log "1. 检查系统基本信息..."
    
    local sys_info="$CHECKUP_DIR/system_info.txt"
    
    echo "=== 系统基本信息 ===" > "$sys_info"
    echo "主机名: $(hostname)" >> "$sys_info"
    echo "检查时间: $(date)" >> "$sys_info"
    echo "运行时间: $(uptime)" >> "$sys_info"
    
    # OS信息
    echo -e "\n--- 操作系统 ---" >> "$sys_info"
    if [ -f /etc/os-release ]; then
        source /etc/os-release
        echo "发行版: $PRETTY_NAME" >> "$sys_info"
        echo "版本: $VERSION" >> "$sys_info"
    else
        echo "OS: $(cat /etc/redhat-release 2>/dev/null || uname -s)" >> "$sys_info"
    fi
    
    echo "内核版本: $(uname -r)" >> "$sys_info"
    echo "架构: $(uname -m)" >> "$sys_info"
    
    # 添加到摘要
    echo "系统: $PRETTY_NAME" >> "$SUMMARY_FILE"
    echo "内核: $(uname -r)" >> "$SUMMARY_FILE"
}

# 2. 硬件资源检查
check_hardware_resources() {
    log "2. 检查硬件资源..."
    
    local hw_info="$CHECKUP_DIR/hardware_info.txt"
    
    echo "=== 硬件资源检查 ===" > "$hw_info"
    
    # CPU信息
    echo -e "\n--- CPU信息 ---" >> "$hw_info"
    echo "物理核心: $(grep 'physical id' /proc/cpuinfo | sort -u | wc -l)" >> "$hw_info"
    echo "逻辑核心: $(nproc)" >> "$hw_info"
    echo "CPU型号: $(grep 'model name' /proc/cpuinfo | head -1 | cut -d: -f2 | sed 's/^[ \t]*//')" >> "$hw_info"
    echo "CPU频率: $(grep 'cpu MHz' /proc/cpuinfo | head -1 | cut -d: -f2 | sed 's/^[ \t]*//') MHz" >> "$hw_info"
    
    # 内存信息
    echo -e "\n--- 内存信息 ---" >> "$hw_info"
    free -h >> "$hw_info"
    
    # 内存详细检查
    echo -e "\n内存详情:" >> "$hw_info"
    grep -E "MemTotal|MemFree|MemAvailable" /proc/meminfo >> "$hw_info"
    
    # 磁盘信息
    echo -e "\n--- 磁盘使用 ---" >> "$hw_info"
    df -h >> "$hw_info"
    
    # 磁盘详细
    echo -e "\n--- 磁盘分区 ---" >> "$hw_info"
    lsblk >> "$hw_info"
    
    # 添加到摘要
    local mem_total=$(grep MemTotal /proc/meminfo | awk '{printf "%.1fG", $2/1024/1024}')
    local disk_total=$(df -h / | awk 'NR==2 {print $2}')
    echo "CPU: $(nproc)核心" >> "$SUMMARY_FILE"
    echo "内存: $mem_total" >> "$SUMMARY_FILE"
    echo "磁盘: $disk_total" >> "$SUMMARY_FILE"
}

# 3. 系统负载检查
check_system_load() {
    log "3. 检查系统负载.."
    
    local load_info="$CHECKUP_DIR/load_info.txt"
    
    echo "=== 系统负载检查 ===" > "$load_info"
    
    # 获取负载信息（兼容各种系统）
    local load_data
    if [ -f /proc/loadavg ]; then
        load_data=$(cat /proc/loadavg)
    else
        # 备用方案：使用uptime命令
        load_data=$(uptime | sed 's/.*load average: //')
    fi
    
    echo "负载数据: $load_data" >> "$load_info"
    
    # 解析负载（1分钟、5分钟、15分钟）
    local load1 load5 load15
    if echo "$load_data" | grep -q " "; then
        load1=$(echo "$load_data" | awk '{print $1}')
        load5=$(echo "$load_data" | awk '{print $2}')
        load15=$(echo "$load_data" | awk '{print $3}')
    else
        # 处理uptime的不同格式
        load1=$(echo "$load_data" | awk -F',' '{print $1}')
        load5=$(echo "$load_data" | awk -F',' '{print $2}')
        load15=$(echo "$load_data" | awk -F',' '{print $3}')
    fi
    
    # 获取CPU核心数（兼容方案）
    local cores
    if command -v nproc >/dev/null 2>&1; then
        cores=$(nproc)
    else
        cores=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || echo "1")
    fi
    
    echo "CPU核心数: $cores" >> "$load_info"
    echo "1分钟负载: $load1" >> "$load_info"
    echo "5分钟负载: $load5" >> "$load_info"
    echo "15分钟负载: $load15" >> "$load_info"
    
    # 改进的负载评估逻辑
    evaluate_load() {
        local load=$1
        local period=$2
        local cores=$3
        
        # 转换为整数比较（避免浮点数问题）
        local load_int=$(echo "$load * 100" | bc 2>/dev/null | cut -d. -f1)
        local cores_int=$((cores * 100))
        
        if [ -z "$load_int" ]; then
            # 如果bc不可用，使用awk处理
            load_int=$(echo "$load" | awk '{printf "%.0f", $1 * 100}')
        fi
        
        if [ $load_int -gt $((cores_int * 2)) ]; then
            # 负载 > 2倍核心数：严重
            warn "$period 负载严重过高: $load (核心数: $cores)"
            echo "❌ $period: 严重过高" >> "$load_info"
        elif [ $load_int -gt $((cores_int * 1)) ]; then
            # 负载 > 核心数：警告
            warn "$period 负载偏高: $load (核心数: $cores)"
            echo "⚠️  $period: 偏高" >> "$load_info"
        elif [ $load_int -gt $((cores_int * 70 / 100)) ]; then
            # 负载 > 70%核心数：注意
            echo "ℹ️  $period: 正常偏高" >> "$load_info"
        else
            # 负载正常
            echo "✅ $period: 正常" >> "$load_info"
        fi
    }
    
    # 评估不同时间段的负载
    echo -e "\n--- 负载评估 ---" >> "$load_info"
    evaluate_load "$load1" "1分钟" "$cores"
    evaluate_load "$load5" "5分钟" "$cores"
    evaluate_load "$load15" "15分钟" "$cores"
    
    # 负载趋势分析
    echo -e "\n--- 负载趋势分析 ---" >> "$load_info"
    if [ $(echo "$load1 > $load5" | bc 2>/dev/null || echo "0") -eq 1 ] && 
       [ $(echo "$load5 > $load15" | bc 2>/dev/null || echo "0") -eq 1 ]; then
        echo "📈 负载趋势: 上升" >> "$load_info"
        warn "系统负载呈上升趋势"
    elif [ $(echo "$load1 < $load5" | bc 2>/dev/null || echo "0") -eq 1 ] && 
         [ $(echo "$load5 < $load15" | bc 2>/dev/null || echo "0") -eq 1 ]; then
        echo "📉 负载趋势: 下降" >> "$load_info"
    else
        echo "➡️  负载趋势: 平稳" >> "$load_info"
    fi
    
    # 进程分析
    echo -e "\n--- 高负载进程 (前10) ---" >> "$load_info"
    if command -v ps >/dev/null 2>&1; then
        echo "PID    %CPU %MEM COMMAND" >> "$load_info"
        ps -eo pid,%cpu,%mem,comm --sort=-%cpu | head -11 >> "$load_info"
    else
        echo "ps命令不可用，跳过进程分析" >> "$load_info"
    fi
    
    # 添加到摘要
    echo "负载: $load1 (1分钟)" >> "$SUMMARY_FILE"
}

# 备用方案：纯bash实现的负载比较（不依赖bc）
compare_load_bash() {
    local load=$1
    local threshold=$2
    
    # 将负载和阈值都乘以100转换为整数
    local load_int=$(echo "$load" | awk -F. '{printf "%d", $1 * 100 + ($2 < 10 ? $2 * 10 : $2)}')
    local threshold_int=$((threshold * 100))
    
    if [ $load_int -gt $threshold_int ]; then
        return 0  # 负载高于阈值
    else
        return 1  # 负载正常
    fi
}

# 4. 网络配置检查
check_network_config() {
    log "4. 检查网络配置..."
    
    local net_info="$CHECKUP_DIR/network_info.txt"
    
    echo "=== 网络配置检查 ===" > "$net_info"
    
    # 网络接口
    echo -e "\n--- 网络接口 ---" >> "$net_info"
    ip addr show >> "$net_info"
    
    # 路由信息
    echo -e "\n--- 路由表 ---" >> "$net_info"
    ip route show >> "$net_info"
    
    # 连接统计
    echo -e "\n--- 连接统计 ---" >> "$net_info"
    ss -s >> "$net_info"
    
    # 端口监听
    echo -e "\n--- 监听端口 (前20) ---" >> "$net_info"
    ss -tuln | head -20 >> "$net_info"
    
    # 网络连通性
    echo -e "\n--- 网络连通性 ---" >> "$net_info"
    ping -c 2 -W 3 8.8.8.8 >/dev/null 2>&1 && echo "外网连通: 正常" >> "$net_info" || warn "外网连通性检查失败"
    
    # DNS检查
    echo -e "\n--- DNS解析 ---" >> "$net_info"
    nslookup google.com >/dev/null 2>&1 && echo "DNS解析: 正常" >> "$net_info" || warn "DNS解析检查失败"
}

# 5. 内核参数检查（通用改进版）
check_kernel_parameters() {
    log "5. 检查内核参数..."
    
    local kernel_info="$CHECKUP_DIR/kernel_params.txt"
    
    echo "=== 内核参数检查 ===" > "$kernel_info"
    
    # 分层参数列表：核心参数 + 可选参数
    local core_params=(
        "net.core.somaxconn"
        "net.ipv4.ip_local_port_range" 
        "fs.file-max"
        "vm.swappiness"
    )
    
    local network_params=(
        "net.core.rmem_max"
        "net.core.wmem_max"
        "net.core.netdev_max_backlog"
        "net.ipv4.tcp_max_syn_backlog"
        "net.ipv4.tcp_rmem"
        "net.ipv4.tcp_wmem"
        "net.ipv4.tcp_mtu_probing"
    )
    
    local advanced_params=(
        "net.nf_conntrack_max"
        "net.netfilter.nf_conntrack_max"
        "net.ipv4.tcp_congestion_control"
        "net.core.default_qdisc"
        "kernel.pid_max"
    )
    
    # 检查sysctl命令可用性
    if ! command -v sysctl >/dev/null 2>&1; then
        error "sysctl命令不可用，跳过内核参数检查"
        echo "sysctl命令不可用" >> "$kernel_info"
        return 1
    fi
    
    # 通用参数检查函数
    check_param() {
        local param=$1
        local value=""
        
        # 多种方式尝试获取参数值
        if value=$(sysctl -n "$param" 2>/dev/null); then
            echo "$param = $value" >> "$kernel_info"
            return 0
        else
            # 尝试直接读取proc文件系统
            local proc_path="/proc/sys/$(echo "$param" | tr '.' '/')"
            if [ -r "$proc_path" ]; then
                value=$(cat "$proc_path" 2>/dev/null)
                echo "$param = $value" >> "$kernel_info"
                return 0
            else
                echo "$param = [不存在或无权限]" >> "$kernel_info"
                return 1
            fi
        fi
    }
    
    # 评估参数值的合理性
    evaluate_param() {
        local param=$1
        local value=$2
        
        case "$param" in
            "net.core.somaxconn")
                if [ "$value" -lt 1024 ] 2>/dev/null; then
                    warn "somaxconn值偏低: $value (建议≥16384)"
                elif [ "$value" -gt 65536 ] 2>/dev/null; then
                    echo "ℹ️  somaxconn值较高: $value" >> "$kernel_info"
                fi
                ;;
                
            "fs.file-max")
                if [ "$value" -lt 100000 ] 2>/dev/null; then
                    warn "文件句柄限制偏低: $value (建议≥100000)"
                fi
                ;;
                
            "net.ipv4.ip_local_port_range")
                local min_port=$(echo "$value" | awk '{print $1}')
                local max_port=$(echo "$value" | awk '{print $2}')
                local port_range=$((max_port - min_port))
                
                if [ "$port_range" -lt 20000 ] 2>/dev/null; then
                    warn "临时端口范围偏小: $port_range (建议≥20000)"
                fi
                ;;
                
            "vm.swappiness")
                if [ "$value" -gt 60 ] 2>/dev/null; then
                    warn "swappiness值偏高: $value (建议10-60)"
                fi
                ;;
        esac
    }
    
    # 检查核心参数
    echo -e "\n--- 核心参数 ---" >> "$kernel_info"
    for param in "${core_params[@]}"; do
        if check_param "$param"; then
            # 获取值成功，进行评估
            local value=$(grep "^$param = " "$kernel_info" | cut -d'=' -f2 | sed 's/^[ \t]*//')
            evaluate_param "$param" "$value"
        fi
    done
    
    # 检查网络参数（可能在某些系统中不存在）
    echo -e "\n--- 网络参数 ---" >> "$kernel_info"
    for param in "${network_params[@]}"; do
        check_param "$param"
    done
    
    # 检查高级参数（可能在新内核中才存在）
    echo -e "\n--- 高级参数 ---" >> "$kernel_info"
    for param in "${advanced_params[@]}"; do
        if ! check_param "$param"; then
            # 参数不存在，记录但不警告
            echo "注: $param 在当前内核中不可用" >> "$kernel_info"
        fi
    done
    
    # 特殊处理：连接跟踪参数（不同系统名称不同）
    echo -e "\n--- 连接跟踪参数 ---" >> "$kernel_info"
    local conntrack_variants=(
        "net.nf_conntrack_max"
        "net.netfilter.nf_conntrack_max"
        "net.ipv4.netfilter.ip_conntrack_max"
    )
    
    local conntrack_found=0
    for param in "${conntrack_variants[@]}"; do
        if check_param "$param"; then
            conntrack_found=1
            local value=$(grep "^$param = " "$kernel_info" | cut -d'=' -f2 | sed 's/^[ \t]*//')
            if [ "$value" -lt 65536 ] 2>/dev/null; then
                warn "连接跟踪限制偏低: $param=$value"
            fi
            break
        fi
    done
    
    if [ "$conntrack_found" -eq 0 ]; then
        echo "未找到连接跟踪参数" >> "$kernel_info"
    fi
    
    # 检查当前连接跟踪状态
    check_conntrack_status() {
    echo -e "\n--- 连接跟踪状态 ---" >> "$kernel_info"
    
    # 可能的状态文件路径
    local count_files=(
        "/proc/sys/net/netfilter/nf_conntrack_count"
        "/proc/sys/net/ipv4/netfilter/ip_conntrack_count"
        "/proc/sys/net/nf_conntrack_count"
    )
    
    # 可能的max参数路径
    local max_files=(
        "/proc/sys/net/netfilter/nf_conntrack_max"
        "/proc/sys/net/ipv4/netfilter/ip_conntrack_max"
        "/proc/sys/net/nf_conntrack_max"
    )
    
    local found=0
    
    # 先尝试通过/proc文件系统获取
    for i in "${!count_files[@]}"; do
        if [ -r "${count_files[i]}" ] && [ -r "${max_files[i]}" ]; then
            local current_count=$(cat "${count_files[i]}")
            local max_count=$(cat "${max_files[i]}")
            
            if [ -n "$current_count" ] && [ -n "$max_count" ] && [ "$max_count" -gt 0 ]; then
                local usage_percent=$((current_count * 100 / max_count))
                echo "当前连接跟踪: $current_count/$max_count ($usage_percent%)" >> "$kernel_info"
                
                if [ "$usage_percent" -gt 80 ]; then
                    warn "连接跟踪使用率过高: $usage_percent%"
                fi
                found=1
                break
            fi
        fi
    done
    
    # 如果/proc方式失败，尝试sysctl
    if [ "$found" -eq 0 ]; then
        local conntrack_variants=(
            "net.nf_conntrack_max"
            "net.netfilter.nf_conntrack_max"
            "net.ipv4.netfilter.ip_conntrack_max"
        )
        
        for param in "${conntrack_variants[@]}"; do
            if max_count=$(sysctl -n "$param" 2>/dev/null); then
                local current_count=$(sysctl -n "$(echo "$param" | sed 's/max$/count/')" 2>/dev/null)
                
                if [ -n "$current_count" ] && [ -n "$max_count" ] && [ "$max_count" -gt 0 ]; then
                    local usage_percent=$((current_count * 100 / max_count))
                    echo "当前连接跟踪: $current_count/$max_count ($usage_percent%)" >> "$kernel_info"
                    
                    if [ "$usage_percent" -gt 80 ]; then
                        warn "连接跟踪使用率过高: $usage_percent%"
                    fi
                    found=1
                    break
                fi
            fi
        done
    fi
    
    if [ "$found" -eq 0 ]; then
        echo "无法获取连接跟踪状态 (可能未加载nf_conntrack模块)" >> "$kernel_info"
    fi
}
    
    check_conntrack_status
    
    # 系统特定检查
    echo -e "\n--- 系统特定信息 ---" >> "$kernel_info"
    
    # 检查容器环境
    if [ -f /.dockerenv ] || grep -q docker /proc/1/cgroup 2>/dev/null; then
        echo "环境: 容器内部" >> "$kernel_info"
        warn "在容器环境中，许多内核参数可能无法修改"
    fi
    
    # 检查内核版本
    local kernel_version=$(uname -r)
    echo "内核版本: $kernel_version" >> "$kernel_info"
    
    # 根据内核版本给出建议
    local major_version=$(echo "$kernel_version" | cut -d. -f1)
    local minor_version=$(echo "$kernel_version" | cut -d. -f2)
    
    if [ "$major_version" -lt 3 ] || { [ "$major_version" -eq 3 ] && [ "$minor_version" -lt 10 ]; }; then
        warn "内核版本较老 ($kernel_version)，建议升级以支持更多优化特性"
    fi
}

# 6. 系统限制检查
check_system_limits() {
    log "6. 检查系统限制..."
    
    local limits_info="$CHECKUP_DIR/limits_info.txt"
    
    echo "=== 系统限制检查 ===" > "$limits_info"
    
    # 文件描述符
    echo -e "--- 文件描述符 ---" >> "$limits_info"
    echo "系统级限制: $(cat /proc/sys/fs/file-nr)" >> "$limits_info"
    echo "进程限制: $(ulimit -n)" >> "$limits_info"
    
    # 用户限制
    echo -e "\n--- 用户限制 ---" >> "$limits_info"
    ulimit -a >> "$limits_info"
    
    # 检查限制是否合理
    local soft_limit=$(ulimit -n)
    [ $soft_limit -lt 10000 ] && warn "文件描述符限制偏低: $soft_limit (建议≥10000)"
}

# 7. 服务状态检查
check_services() {
    log "7. 检查服务状态..."
    
    local services_info="$CHECKUP_DIR/services_info.txt"
    
    echo "=== 服务状态检查 ===" > "$services_info"
    
    # 关键服务列表
    local important_services=(
        "sshd"
        "systemd-resolved"
        "networking"
        "systemd-networkd"
    )
    
    for service in "${important_services[@]}"; do
        if systemctl is-active $service >/dev/null 2>&1; then
            echo "✅ $service: 运行中" >> "$services_info"
        else
            echo "❌ $service: 未运行" >> "$services_info"
        fi
    done
    
    # 检查代理相关端口
    echo -e "\n--- 代理端口检查 ---" >> "$services_info"
    if ss -tuln | grep ":2510" >/dev/null; then
        echo "✅ 代理端口2510: 监听中" >> "$services_info"
    else
        warn "代理端口2510未监听"
    fi
}

# 8. 安全配置检查
check_security() {
    log "8. 检查安全配置..."
    
    local security_info="$CHECKUP_DIR/security_info.txt"
    
    echo "=== 安全配置检查 ===" > "$security_info"
    
    # SSH配置
    echo -e "--- SSH配置 ---" >> "$security_info"
    if [ -f /etc/ssh/sshd_config ]; then
        grep -E "^Port|^PermitRootLogin|^PasswordAuthentication" /etc/ssh/sshd_config >> "$security_info"
    fi
    
    # 防火墙状态
    echo -e "\n--- 防火墙状态 ---" >> "$security_info"
    if command -v ufw >/dev/null; then
        ufw status >> "$security_info"
    elif command -v firewall-cmd >/dev/null; then
        firewall-cmd --state >> "$security_info"
    else
        echo "未检测到防火墙" >> "$security_info"
    fi
    
    # 最近登录
    echo -e "\n--- 最近登录 ---" >> "$security_info"
    last -5 >> "$security_info"
}

# 9. 生成检查报告
generate_report() {
    log "9. 生成检查报告..."
    
    # 合并详细报告
    cat $CHECKUP_DIR/*.txt > "$DETAIL_REPORT" 2>/dev/null
    
    # 生成摘要
    echo -e "\n=== 检查摘要 ===" >> "$SUMMARY_FILE"
    echo "检查完成时间: $(date)" >> "$SUMMARY_FILE"
    
    # 问题统计
    local warn_count=$(grep -c "WARN:" "$ISSUES_FILE" 2>/dev/null || echo "0")
    local error_count=$(grep -c "ERROR:" "$ISSUES_FILE" 2>/dev/null || echo "0")
    
    echo "发现问题: $warn_count 个警告, $error_count 个错误" >> "$SUMMARY_FILE"
    
    if [ $warn_count -gt 0 ] || [ $error_count -gt 0 ]; then
        echo -e "\n=== 建议优先处理的问题 ===" >> "$SUMMARY_FILE"
        grep -E "WARN:|ERROR:" "$ISSUES_FILE" | head -10 >> "$SUMMARY_FILE"
    else
        echo "未发现严重问题" >> "$SUMMARY_FILE"
    fi
    
    # 最终建议
    echo -e "\n=== 优化建议 ===" >> "$SUMMARY_FILE"
    echo "1. 根据详细报告调整内核参数" >> "$SUMMARY_FILE"
    echo "2. 监控系统负载和资源使用" >> "$SUMMARY_FILE"
    echo "3. 定期检查安全配置" >> "$SUMMARY_FILE"
    echo "4. 备份重要配置" >> "$SUMMARY_FILE"
}

# 主检查流程
main_checkup() {
    log "开始机器全面检查..."
    log "检查结果保存到: $CHECKUP_DIR"
    
    init_checkup
    
    # 执行各项检查
    check_system_basic
    check_hardware_resources
    check_system_load
    check_network_config
    check_kernel_parameters
    check_system_limits
    check_services
    check_security
    
    generate_report
    
    # 显示检查结果
    echo -e "\n${GREEN}=== 检查完成 ===${NC}"
    echo -e "${CYAN}摘要报告:${NC} $SUMMARY_FILE"
    echo -e "${CYAN}详细报告:${NC} $DETAIL_REPORT"
    echo -e "${CYAN}问题列表:${NC} $ISSUES_FILE"
    
    # 显示关键信息
    echo -e "\n${YELLOW}=== 关键信息 ===${NC}"
    tail -20 "$SUMMARY_FILE"
    
    if [ -s "$ISSUES_FILE" ]; then
        echo -e "\n${RED}=== 发现的问题 ===${NC}"
        cat "$ISSUES_FILE"
    fi
    
    log "检查完成！请查看报告文件获取详细信息。"
}

# 脚本执行
if [[ $1 == "--help" ]]; then
    echo "使用方法: $0"
    echo "功能: 全面检查机器配置和状态"
    echo "输出: 在/tmp/machine_checkup_时间戳/ 目录下生成报告"
    exit 0
fi

main_checkup
